<?php

// <editor-fold defaultstate="collapsed" desc="管理员菜单级权限">
//管理员菜单---------------------------------------------------------------开始
/**
 * 
 * @param int $pid      //父ID
 * @param int $status   //菜单状态
 * @param int $cache    //缓存时间
 * @return array        //返回管理菜单列表数组
 */
function getAdminMenu($pid = 0, $status = 1, $cache = null) {
    $list = getAdminMenuAll($cache);
    $array = array();
    foreach ($list as $m) {
        if ($m['pid'] == $pid && $m['status'] == $status) {
            array_push($array, $m);
        }
    }
    return $array;
}

function getAdminMenuAll($cache_time = null) {
    $cache_key = 'admin_menu';
    if (S($cache_key)) {
        $list = (array) S($cache_key);
        return $list;
    } else {
        $list = M('admin_menu')->order('sort asc')->select();
        if (!empty($cache_time)) {
            S($cache_key, $list, $cache_time);
            return $list;
        } else {
            return $list;
        }
    }
}

//管理员菜单---------------------------------------------------------------结束
//管理权限------------------------------------------------------------开始
function getAdminPower($power = null) {
    return '超级管理员';
//    $str = null;
//    $list = array();
//    if (S('admin_power')) {
//        $list = S('admin_power');
//    } else {
//        $list = M('admin_power')->where('status=1')->select();
//        S('admin_power', $list);
//    }
//    foreach ($list as $m) {
//        if (($power & $m['value']) == $m['value']) {
//            $str.=$m['name'] . '，';
//        }
//    }
//    return rtrim($str, "，");
}

//管理权限------------------------------------------------------------结束
// <editor-fold defaultstate="collapsed" desc="递归分类树">

function getCacheTree($cache = null, $table = null) {
    $tablename = ucfirst($table);
//    $json = S($cache_key);
//    if (!empty($json)) {
//        return $json;
//    } else {
    $array = array();
    $array['id'] = "0";
    $array['text'] = "全部类型";
    $array['state'] = array('opened' => false);
    $array['children'] = array();
    $array['children'] = getTree(0, $tablename);
    $json = json_encode($array);


    if (isset($cache)) {
        S($cache_key, $json, $cache);
    } else {
        $global_cache_time = C('GLOBAL_CACHE_TIME');
        if ($global_cache_time) {
            S($cache_key, $json, $global_cache_time);
        }
    }

    return $json;
//    }
}

//无缓存递归
function getTree($pid = 0, $table = null) {
    $tablename = ucfirst($table);
    $list = M($tablename)->where(array('p_id' => $pid))->order('sort asc')->select();

    $array_tree = array();
    foreach ($list as $m) {
        $array = array();
        $array['id'] = $m['id'];
        //if ($pid > 0) {
        $array['parent'] = $m['p_id'];
        //}
        if ($m['c_code'] === "") {
            $array['text'] = $m['name'];
        } else {
            $array['text'] = '(' . $m['c_code'] . ')' . $m['name'];
        }
        //$array['icon'] = 'jstree-file';
        $array['state'] = array('opened' => false);
        $array_son = getTree($m['id'], $tablename);
        if (count($array_son) > 0) {
            $array['children'] = array();
            //array_push($array['children'], getTree($m['id']));
            $array['children'] = $array_son;
        }
        array_push($array_tree, $array);
    }
    return $array_tree;
}

//无缓存递归
function getRootTree($pid = 0, $table = null) {
    $tablename = ucfirst($table);
    $list = M($tablename)->where(array('p_id' => $pid))->order('sort asc')->select();
    $array_tree = array();
    foreach ($list as $m) {
        $array = array();
        $array['id'] = $m['id'];
        if ($pid > 0) {
            $array['parent'] = $m['p_id'];
        }
        $array['text'] = $m['name'];
        //$array['icon'] = 'jstree-file';
        $array['state'] = array('opened' => false);
        $array_son = getTree($m['id']);
        if (count($array_son) > 0) {
            $array['children'] = array();
            //array_push($array['children'], getTree($m['id']));
            $array['children'] = $array_son;
        }
        array_push($array_tree, $array);
    }
    return $array_tree;
}

function getJsonTree() {
    return;
}

// </editor-fold>